/*
 * The MIT License
 *
 * Copyright 2012 Anonymous.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package pentomino.logic;

/**
 * 
 * Represents a pentomino game figure.
 */
public class Figure extends BitField {
    
    protected char letter; // A corresponding letter.

    /*============== Constructors ==============*/
    
    /**
     * Creates an instance of the Figure class by the specified letter (symbol).
     * 
     * @param letter a corresponding character
     */
    public Figure(char letter)
    {
        super(5, 5);
        
        char c = Character.toUpperCase(letter); 
        int cdata = generateData(c);
        
        if (cdata == 0)
            throw new IllegalArgumentException(
                            String.format("Unexpected symbol '%c'.", letter)
                      );
        
        data = cdata;
        this.letter = c;
    }
    
    /*============== Member access ==============*/
    
    /**
     * Gets a figure type.
     * 
     * @return letter (symbol)
     */
    public char getType()
    {
        return letter;
    }
    
    /*============== Methods ==============*/
    
    /**
     * Determines whether the figure's type is a specified type of the figures.
     * 
     * @param letter a corresponding character
     * @return result of the comparsion
     */
    public boolean is(char letter)
    {
        return this.letter == letter;
    }
    
    /**
     * Rotates the figure by 90 degrees counterclockwise.
     */
    public void rotateLeft()
    {
        if (rows != cols)
            throw new UnsupportedOperationException("Operation can be applied only for square matrices.");
        
        int dim = rows;
        
        for (int i = 0; i < dim / 2; i++)
        {
            for (int j = i; j < dim - 1 - i; j++)
            {
                byte temp = bitAt(i, j);
                
                setBitAt(i, j, bitAt(j, dim - 1 - i));
                setBitAt(j, dim - 1 - i, bitAt(dim - 1 - i, dim - 1 - j));
                setBitAt(dim - 1 - i, dim - 1 - j, bitAt(dim - 1 - j, i));
                setBitAt(dim - 1 - j, i, temp);
            }
        }
    }
    
    /**
     * Rotates the figure by 90 degrees clockwise.
     */
    public void rotateRight()
    {
        if (rows != cols)
            throw new UnsupportedOperationException("Operation can be applied only for square matrices.");
        
        int dim = rows;
        
        for (int i = 0; i < dim / 2; i++)
        {
            for (int j = i; j < dim - 1 - i; j++)
            {
                byte temp = bitAt(i, j);
                
                setBitAt(i, j, bitAt(dim - 1 - j, i));
                setBitAt(dim - 1 - j, i, bitAt(dim - 1 - i, dim - 1 - j));
                setBitAt(dim - 1 - i, dim - 1 - j, bitAt(j, dim - 1 - i));
                setBitAt(j, dim - 1 - i, temp);
            }
        }
    }
    
    /**
     * Resets the figure state.
     */
    public void reset()
    {
        data = generateData(letter);
    }
        
    /*============== Static methods ==============*/
    
    /**
     * Generates a default date for matrix filling.
     * 
     * @param letter a corresponding character
     * @return a sequence of bits as an integer value
     */
    protected static int generateData(char letter)
    {
        switch (letter)
        {
            case 'F':
                return 209024; // 0b0000000110011000010000000
            case 'I':
                return 4329604; // 0b0010000100001000010000100
            case 'L':
                return 4329664; // 0b0010000100001000011000000
            case 'N':
                return 71812; // 0b0000000010001100010000100
            case 'P':
                return 71872; // 0b0000000010001100011000000
            case 'T':
                return 462976; // 0b0000001110001000010000000
            case 'U':
                return 10688; // 0b0000000000010100111000000
            case 'V':
                return 270784; // 0b0000001000010000111000000
            case 'W':
                return 274624; // 0b0000001000011000011000000
            case 'X':
                return 145536; // 0b0000000100011100010000000
            case 'Y':
                return 143492; // 0b0000000100011000010000100
            case 'Z':
                return 397504; // 0b0000001100001000011000000
            default:
                return 0;
        }
    }
    
    
}
